package com.asion.qkan.ui.model;

import com.asion.qkan.api.ServiceApi;
import com.asion.qkan.entry.HomeBean;
import com.asion.qkan.ui.contract.HomeContract;
import com.asion.qkan.utils.retrofitCache.SetCacheUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by asion on 16-10-12.
 */

public class HomeModel implements HomeContract.Model {

    @Override
    public void getHomeDatas(String category, String id,HomeContract.OnHomeGetListener onHomeGetListener) {
        //请求数据
        new Retrofit.Builder()
                .baseUrl("http://api.kkmh.com/")
                .client(SetCacheUtils.setCache())
                .addConverterFactory(GsonConverterFactory.create())//创建Gson解析的Factory
                .build()
                .create(ServiceApi.class)
                .getHomeCommend(category,id)//每个Fragment请求的不一样
                .enqueue(new Callback<HomeBean>() {
                    @Override
                    public void onResponse(Call<HomeBean> call, Response<HomeBean> response) {
                        HomeBean homeBean = response.body();
                        onHomeGetListener.onHomeSuccess(homeBean);
                    }

                    @Override
                    public void onFailure(Call<HomeBean> call, Throwable t) {
                        onHomeGetListener.onHomeError(t);
                    }
                });

    }
}
